<template>
  <div class='container'>
    <el-card class="box-card">
      <!-- 顶部搜索栏 -->
       <el-form ref="form"  :inline="true" :model="form" label-width="80px" label-position='right' >
        <el-row >
          <el-col :span="6">
            <el-form-item label="标签名称" style="width:100%">
              <el-input style="width:100%" size="small" v-model.trim="form.abbreviation"  placeholder="根据标签搜索"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市">
                <el-select style="width:100%" v-model="form.province" placeholder="请选择" @change="provincesChange(form.province)" size="small">
                  <el-option
                    v-for="item in citySelect.province"
                    :key="item"
                    :label="item"
                    :value="item">
                  </el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="地区">
              <el-select style="width:100%" v-model="form.citys" placeholder="请选择" size="small">
                  <el-option
                    v-for="item in citySelect.cityDate"
                    :key="item"
                    :label="item"
                    :value="item">
                  </el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item  label="企业简称" size="small">
              <el-input style="width:100%" v-model.trim="form.keyword"  placeholder="企业简称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="状态">
              <el-select style="width:100%" v-model="form.state" placeholder="请选择" size="small">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="padding-left: 40px">
              <el-button  @click="resetForm('numberValidateForm')" size="small">清除</el-button>
              <el-button type="primary" @click="loadList" size="small">搜索</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="success-button">
            <el-button type="success" icon="el-icon-edit" size="small"  @click="onAdd" >新增技巧</el-button>
          </el-col>
        </el-row>
      </el-form>
      <!-- 提示文案 -->
      <el-alert
        type="info"
        show-icon>
        <template slot="title">
          数据一共{{total}} 条
        </template>
      </el-alert>
      <!-- 表格 -->
      <el-table
        :data="tableData"
        v-loading="loading"
        style="width: 100; margin: 10px 0;">
        <el-table-column
          prop="id"
          label="序号"
          width="100">
        </el-table-column>
        <el-table-column
          prop="number"
          label="企业编号">
        </el-table-column>
        <el-table-column
          prop="shortName"
          label="企业简称">
        </el-table-column>
        <el-table-column
          prop="tags"
          label="标签">
        </el-table-column>
        <el-table-column
          prop="creatorID"
          label="创建者">
        </el-table-column>
        <el-table-column
          label="创建时间">
          <template slot-scope="{row}">
            {{row.createTime | date}}
          </template>
        </el-table-column>
        <el-table-column
          prop="remarks"
          label="备注">
        </el-table-column>
        <el-table-column
          label="状态">
          <template slot-scope="{row}">
            {{row.state===1?'启用':'禁用'}}
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          width="180"
          label="操作">
          <template slot-scope="{row}">
            <el-button plain type="primary" icon="el-icon-edit" @click="onEdit(row)"  circle></el-button>
            <el-tooltip class="item" effect="dark" :content="row.state===0?'启用':'禁用'" placement="top">
              <el-button plain type="success" :icon="row.state===1?'el-icon-close':'el-icon-check'" circle @click="disabled(row)"></el-button>
            </el-tooltip>
            <el-button plain type="danger" icon="el-icon-delete" @click="onDelete(row)" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <page-nation :total='total' :formlist='formList' @on-success='onChangePage'/>
      <!-- 预览 -->

      <!-- 新增 -->
      <companys-add :dialogFormVisible.sync='editDialogVisible' :formBase='row' :titleInfo='titleInfo' @on-success="loadList" />
    </el-card>
  </div>
</template>

<script>
import PageNation from '../components/PageNation.vue'
import { list, remove, disabled } from '@/api/hmmm/companys.js'
import dayjs from 'dayjs'
import { provinces, citys } from '@/api/hmmm/citys.js' // 省份
import CompanysAdd from '../components/companys-add.vue'
export default {
  components: { PageNation, CompanysAdd },
  data () {
    return {
      citySelect: {
        province: [],
        cityDate: []
      },
      form: {
        keyword: '',
        province: '',
        state: '',
        abbreviation: '',
        citys: ''
      },
      loading: true,
      tableData: [],
      formList: {
        page: 1,
        pagesize: 10 // 页尺寸
      },
      total: 1, // 共几页,
      editDialogVisible: false, // 预览
      titleInfo: {
        text: '',
        pageTitle: '用户'
      },

      row: {},
      options: [
        {
          value: '1',
          label: '启用'
        },
        {
          value: '0',
          label: '禁用'
        }
      ],
      provinces: [],
      citys: []
    }
  },
  created () {
    this.loadList()
    this.getCityData()
  },
  methods: {
    // 加载数据
    async loadList () {
      this.loading = true
      const obj = { ...this.formList }
      for (const key in this.form) {
        if (this.form[key] !== '') {
          obj[key] = this.form[key]
        }
      }
      const res = await list(obj)
      this.loading = false
      this.tableData = res.data.items
      this.total = res.data.counts
      this.row = {}
    },
    // 更改页数
    async onChangePage (val) {
      this.formList.page = val.pagenum
      this.formList.pagesize = val.pagesize
      this.loadList()
    },
    // 重置
    resetForm () {
      this.$refs.form.resetFields()
      this.form = {}
    },
    // 预览
    onPreview (row) {
      this.row = row
      this.previewDialogVisible = true
    },
    // 添加
    onAdd () {
      this.editDialogVisible = true
      this.row = {}
    },
    // 编辑
    onEdit (row) {
      this.editDialogVisible = true
      this.row = row
    },
    async onDelete (row) {
      await remove({
        id: row.id
      })
      this.$message.success('删除成功')
      this.loadList()
    },
    // 状态修改
    async disabled (row) {
      row.state = row.state === 1 ? 0 : 1
      await disabled({
        id: row.id,
        state: row.state
      })
      this.$message.success('操作成功')
    },
    // // 视频
    // showVideo (row) {
    //   this.videoDialog = true
    //   this.rowUrl = row.videoURL
    // },
    // 改变省份
    getCityData: function () {
      this.citySelect.province = provinces()
    },
    // 选省获取到市
    provincesChange: function (e) {
      this.citySelect.cityDate = citys(e)
      this.form.citys = this.citySelect.cityDate[0]
    }
  },
  filters: {
    date (val) {
      return dayjs(dayjs(val).$d).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style scoped lang='less'>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}

.box-card {
  // width: 100%;
  margin: 10px;
  // box-sizing: border-box;
}
.success-button {
  text-align: right;
}
::v-deep .el-form-item {
  width: 100%;
  box-sizing: border-box;
  display: flex;
  .el-form-item__content {
    flex: 1;
  }
}
</style>
